var flag = 1;
$(function(){
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
});
function updateModule(th,type){
    if(type == 1){
        //修改显示
        var is_show = $(th).attr("data") == 1 ? 0 : 1;
        show(th,is_show,$(th).attr("data-module-id"));
    }else if(type == 2){
        //修改排序
        var newsort = $(th).val();
        var oldsort = $(th).attr('data');
        if(newsort == oldsort){
            return false;
        }
        changeSort(th,newsort,$(th).attr("data-module-id"));
    }else{
        //删除
        if(confirm('确认删除吗？')){
            deleteModule(th,$(th).attr("data-module-id"));
        }
    }
}
function show(th,is_show,find_module_id){
    if(flag == 1){
        flag = 0;
        $.ajax({
            type: "POST",
            url: "backstage/module/show",
            data:{"is_show":is_show,"find_module_id":find_module_id},
            success: function(msg){
                flag = 1;
                if(msg.state == "M00000"){
                    $(th).attr("data",msg.result.is_show);
                    showMessage("",msg.message);
                }else{
                    showMessage(msg.message,"");
                }
            },
            error:function(){
                flag = 1;
                showMessage("修改失败!","");
            }
        });
    }
}
function changeSort(th,sort,find_module_id){
    if(flag == 1){
        flag = 0;
        $.ajax({
            type: "POST",
            url: "backstage/module/update-sort",
            data:{"sort":sort,"find_module_id":find_module_id},
            success: function(msg){
                flag = 1;
                if(msg.state == "M00000"){
                    $(th).attr("data",msg.result.sort);
                    showMessage("",msg.message);
                }else{
                    showMessage(msg.message,"");
                }
            },
            error:function(){
                flag = 1;
                showMessage("修改失败!","");
            }
        });
    }
}
function deleteModule(th,find_module_id){
    if(flag == 1){
        flag = 0;
        $.ajax({
            type: "POST",
            url: "backstage/module/delete",
            data:{"find_module_id":find_module_id},
            success: function(msg){
                flag = 1;
                if(msg.state == "M00000"){
                    showMessage("",msg.message);
                    window.location.reload();
                }else{
                    showMessage(msg.message,"");
                }
            },
            error:function(){
                flag = 1;
                showMessage("修改失败!","");
            }
        });
    }
}